﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
@section Content{

}
<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
            @*<a class="btn btn-default" onclick="$.reload()">自拓场地todo</a>*@
        </div>
        @*<div class="btn-group">
                <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-eraser"></i>添加按钮</a>
            </div>
            <div class="operate">
                <ul class="nav nav-pills">
                    <li class="first">已选中<span>1</span>项</li>
                    <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改按钮</a></li>
                    <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除按钮</a></li>
                </ul>
                <a href="javascript:;" class="close"></a>
            </div>
            <script>$('.toolbar').authorizeButton()</script>*@
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询活动名称" style="width: 200px;">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>


<script>
    var citys = [];
    $(function () {
        GetCitys();
        gridList();
    })

    function GetCitys() {
        $.ajax({
            url: "/OrderFlowManage/ClientOrder/GetCitysJson",
            data: {},
            dataType: "json",
            async: false,
            success: function (data) {
                citys = data;
            }
        });
    }

    function gridList() {
        var $gridList = $("#gridList");
        var flowStateArr = ['订单指派', '场地推荐', '带看场地', '意向场地', '价格谈判', '场地预定', '签订合同', '订单完成'];
        var flowOrderStateArr = ['等待确认', '通过', '驳回'];
        var stateArr = ['无效', '有效', '信息不完整'];
        var ChangeReason = ["", "订单太多，忙不过来", "销售活动执行中无时间跟进", "场地库无可匹配场地储备", "场地无利润", "活动时间太近来不及跟近", "没接触过此类型活动"];
        $gridList.dataGrid({
            url: "/OrderFlowManage/ClientOrder/GetGridJson",
            height: $(window).height() - 126,
            colModel: [
                { label: '编号', name: 'F_Id', hidden: true },                
                {
                    label: '操作', name: 'F_Id', width: 50, align: 'center', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        return '<div class="btn-group btn-group-xs" style="margin-top:5px;">'
                            + '   <button type="button" class="btn btn-link" onclick="btn_giveBackSingle(\'' + cellvalue + '\')">抢单</button>'
                            + '</div>';
                    }
                },
                {
                    label: '活动城市', name: 'F_ActivityCityId', width: 50, sortable: false, align: 'left', formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue) {
                            var arr = cellvalue.indexOf(',') == -1 ? [cellvalue] : cellvalue.split(',');
                            var result = [];
                            for (var i = 0; i < citys.length; i++) {
                                var ele = citys[i];
                                for (var j = 0; j < arr.length; j++) {
                                    var val = arr[j];
                                    if (val == ele.Value) {
                                        result.push(ele.Text);
                                    }
                                }
                            }
                            return result.toString();
                        }
                        return '';
                    }
                },
                {
                    label: '活动日期', name: 'F_ActivityStartDate', width: 150, sortable: false, align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var start = cellvalue ? cellvalue.split(' ')[0] : '';
                        var end = rowObject.F_ActivityEndDate ? rowObject.F_ActivityEndDate.split(' ')[0] : '';
                        return start + '~' + end;
                    }
                },
                { label: '活动主题', name: 'F_ActivityTheme', width: 100, sortable: false, align: 'left' },
                { label: '活动类型', name: 'F_SiteTypeName', width: 80, sortable: false, align: 'left' },
                {
                    label: '预算金额(元)', name: 'F_DemandCash', width: 80, align: 'left', sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        ;
                        return (cellvalue || "").toString();
                    }
                },
                { label: '场地大小(㎡)', name: 'F_SiteArea', width: 100, sortable: false, align: 'left' },
                {
                    label: '联系人', name: 'F_Contact', width: 80, sortable: false, align: 'left', formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue.length > 0) {
                            return cellvalue.toString().substring(0, 1) + "**";
                        } else {
                            return cellvalue;
                        }
                    }
                },
                { label: '公司名称', name: 'F_ClientName', width: 100, sortable: false, align: 'left' },
                //{
                //    label: '订单状态', name: 'F_OrderState', width: 80, align: 'left',
                //    formatter: function (cellvalue, options, rowObject) {
                //        // 0指派1场地推荐2带看场地3意向场地4价格谈判5场地预定6签订合同7订单完成
                //        return flowStateArr[cellvalue];
                //    }
                //},
                //{
                //    label: '订单进度', name: 'F_OrderStateProcess', width: 80, align: 'left',
                //    formatter: function (cellvalue, options, rowObject) {
                //        // -1 等待跟进  0等待确认    1通过    2驳回
                //        return cellvalue == -1 ? "等待跟进" : flowOrderStateArr[cellvalue];
                //    }
                //},
                {
                    label: '状态', name: 'F_State', width: 50, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        // 订单状态:0:无效,1:有效，2：信息不完整
                        return stateArr[cellvalue];
                    }
                },
                { label: '跟进人', name: 'F_FlowUserName', width: 80, sortable: false, align: 'left' },
                {
                    label: '关闭理由', name: 'F_ChangeReason', width: 150, sortable: false, align: 'left',
                    formatter: function (cellvalue, options, rowObject) {
                        if (cellvalue) {
                            if (ChangeReason[cellvalue - 0])
                                return ChangeReason[cellvalue - 0];
                            else
                                return "";
                        }
                        else
                            return "";
                    }
                },
                {
                    label: '更新时间', name: 'F_LastModifyTime', width: 120, align: 'center',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' }
                },
                {
                    label: '创建时间', name: 'F_CreatorTime', width: 120, align: 'center',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i', newformat: 'Y-m-d H:i' }
                }
            ],
            postData: {
                environment: 2,
                type: "all"
            },
            pager: "#gridPager",
            sortname: 'F_State, F_OrderState, F_LastModifyTime',
            viewrecords: true
        });
        $("#btn_search").click(function () {
            var queryJson = {
                keyword: $("#txt_keyword").val(), orderstate: $("#orderstate").val()
            };
            $gridList.jqGrid('setGridParam', {
                postData: { queryJson: JSON.stringify(queryJson) },
            }).trigger('reloadGrid');
        });
    }
    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增 ClientOrder",
            url: "/OrderFlowManage/ClientOrder/Form",
            width: "500px",
            height: "300px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Form",
            title: "修改 ClientOrder",
            url: "/OrderFlowManage/ClientOrder/Form?keyValue=" + keyValue,
            width: "700px",
            height: "520px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_delete() {
        $.deleteForm({
            url: "/OrderFlowManage/ClientOrder/DeleteForm",
            param: { keyValue: $("#gridList").jqGridRowValue().F_Id },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        })
    }

    // 让单
    function btn_giveSingle(id) {
        $.modalOpen({
            id: "GiveSingle",
            title: "让单",
            url: "/OrderFlowManage/ClientOrder/GiveSingle?orderid=" + id,
            width: "700px",
            height: "420px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

    // 接单
    function btn_giveBackSingle(keyValue) {
        $.modalConfirm('抢单需要部门负责人审核，审核通过后，可在【有效需求】中查看？', function (flag) {
            if (flag) {
                $.submitForm({
                    url: "/OrderFlowManage/ClientOrder/SubmitSignInClientOrderForm?keyValue=" + keyValue,
                    param: {},
                    success: function () {
                        $("#gridList").trigger("reloadGrid");
                    }
                })
            }
        });
    }

    function btn_detail(id, type) {
        $.addTab({
            href: '/OrderFlowManage/ClientOrder/Details?orderid=' + id + '&type=' + type,
            text: '订单详细',
            id: id,
            index: 999
        });
    }

    function btn_statedetail(id, type) {
        if (type == 0) {
            $.modalOpen({
                id: "GiveSingle",
                title: "订单指派",
                url: "/OrderFlowManage/ClientOrder/GiveSingle?orderid=" + id,
                width: "700px",
                height: "420px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitOrderForm();
                }
            });
        } else {
            btn_detail(id, type);
        }
    }

    function search_orderState(dom, state) {
        if ($(dom).hasClass("active")) {
            $("#orderstate").val('');
            $("#search_orderState .btn").removeClass("active");
        } else {
            $("#orderstate").val(state);
            $("#search_orderState .btn").removeClass("active");
            $(dom).addClass("active");
        }
    }
</script>
